home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / Ph 1.1.1 / PhClient / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-03-08  |  3.2 KB  |  150 lines  |  [TEXT/MPS ]

  1. /***********************************************************************/
  2. /*********************************************************************
  3. * This software is Copyright (C) 1988 by Steven Dorner and the
  4. * University of Illinois Board of Trustees, and by CSNET.  No warranties of
  5. * any kind are expressed or implied.  No support will be provided.
  6. * This software may not be redistributed without prior consent of CSNET.
  7. * You may direct questions to dorner@garcon.cso.uiuc.edu.
  8. **********************************************************************/
  9.  
  10. #define ROTORSZ 256
  11. #define MASK 0377
  12.  
  13. #include <string.h>
  14. #include <crypt.h>
  15.  
  16. char  t1[ROTORSZ];
  17. char  t2[ROTORSZ];
  18. char  t3[ROTORSZ];
  19.  
  20. int   Encrypting = 0;
  21. static int ccnt, nchars, n1, n2;
  22. char   *Visible();
  23. int crypt_init(char *pw);
  24. int encode(char *out, char *buf, int n);
  25. int threecpy(unsigned char *to, unsigned char *from);
  26.  
  27.  
  28. crypt_start(pass)
  29. char   *pass;
  30. {
  31.  
  32.   n1 = 0;
  33.   n2 = 0;
  34.   ccnt = 0;
  35.   nchars = 0;
  36.   Encrypting = 1;
  37.   crypt_init(pass);
  38. }
  39.  
  40. crypt_init(pw)
  41. char   *pw;
  42. {
  43.   int   ic, i, k, temp;
  44.   unsigned random;
  45.   char  buf[13];
  46.   int  seed;
  47.   char   *crypt();
  48.  
  49.   /* must reinitialize the arrays */
  50.   for (i = 0; i < ROTORSZ; i++)
  51.     t1[i] = t2[i] = t3[i] = 0;
  52.  
  53.   strncpy(buf, crypt(pw, pw), 13);
  54.  
  55.   seed = 123;
  56.   for (i = 0; i < 13; i++)
  57.     seed = seed * buf[i] + i;
  58.   for (i = 0; i < ROTORSZ; i++)
  59.     t1[i] = i;
  60.   for (i = 0; i < ROTORSZ; i++)
  61.   {
  62.     seed = 5 * seed + buf[i % 13];
  63.     random = seed % 65521;
  64.     k = ROTORSZ - 1 - i;
  65.     ic = (random & MASK) % (k + 1);
  66.     random >>= 8;
  67.     temp = t1[k];
  68.     t1[k] = t1[ic];
  69.     t1[ic] = temp;
  70.     if (t3[k] != 0)
  71.       continue;
  72.     ic = (random & MASK) % k;
  73.     while (t3[ic] != 0)
  74.       ic = (ic + 1) % k;
  75.     t3[k] = ic;
  76.     t3[ic] = k;
  77.   }
  78.   for (i = 0; i < ROTORSZ; i++)
  79.     t2[t1[i] & MASK] = i;
  80. }
  81.  
  82. /***********************************************************************
  83. * encrypts a string
  84. * first byte of string is encoded length of string
  85. * returns length of encoded string
  86. ***********************************************************************/
  87. encryptit(to, from)
  88. char   *to;
  89. char   *from;
  90. {
  91.   char  scratch[4096];
  92.   char   *sp;
  93.  
  94.   sp = scratch;
  95.   for (; *from; from++)
  96.   {
  97.     *sp++ = t2[(t3[(t1[(*from + n1) & MASK] + n2) & MASK] - n2) & MASK] - n1;
  98.     n1++;
  99.     if (n1 == ROTORSZ)
  100.     {
  101.       n1 = 0;
  102.       n2++;
  103.       if (n2 == ROTORSZ)
  104.     n2 = 0;
  105.     }
  106.   }
  107.   return (encode(to, scratch, sp - scratch));
  108. }
  109.  
  110. /*
  111.  * * Basic 1 character encoding function to make a char printing. 
  112.  */
  113. #define ENC(c) (((unsigned char)(c) & 077) + '#')
  114.  
  115. encode(out, buf, n)       /* output a line of binary (up to 45 chars)
  116.          * in */
  117. int   n;            /* a readable format, converting 3 chars to 4  */
  118. char   *buf;
  119. char   *out;
  120. {
  121.   int   i;
  122.   char   *outStart;
  123.  
  124.   outStart = out;
  125.   *out++ = ENC(n);
  126.  
  127.   for (i = 0; i < n; buf += 3, i += 3, out += 4)
  128.     threecpy((unsigned char *)out, (unsigned char *)buf);
  129.  
  130.   /* null terminate */
  131.   *out = '\0';
  132.   return (out - outStart);
  133. }
  134.  
  135. threecpy(to, from)
  136. unsigned char *to;
  137. unsigned char *from;
  138. {
  139.   int   c1, c2, c3, c4;
  140.  
  141.   c1 = *from >> 2;
  142.   c2 = (*from << 4) & 060 | (from[1] >> 4) & 017;
  143.   c3 = (from[1] << 2) & 074 | (from[2] >> 6) & 03;
  144.   c4 = from[2] & 077;
  145.   *to++ = ENC(c1);
  146.   *to++ = ENC(c2);
  147.   *to++ = ENC(c3);
  148.   *to = ENC(c4);
  149. }
  150.